﻿using System;
using System.Collections;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Texpro
{
    public partial class FMTR202 : Form
    {
        
        public FMTR202()
        {
            InitializeComponent();
        }

        private void yarn_po_Load(object sender, EventArgs e)
        {
            lbl_form_title.Text = this.Text;
            load_form();
            clear_form();
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            UseWaitCursor = true;
            if (validate_form() == true)
            {
                if (save_form() == true)
                {
                    UseWaitCursor = false;
                    PCMSG.Show("Record saved successfully. Dyeing Inward Id is " + PCPUB.m_dyeing_iw_id.ToString() + ".");
                    UseWaitCursor = true;
                    PCPUB.m_dyeing_iw_id = String.Empty;
                    clear_form();
                    lst_dyer.Focus();
                    switch (PCPUB.m_entry_form_mode)
                    {
                        case "ADDNEW":
                            break;
                        case "EDIT":
                            this.Close();
                            break;
                    }

                }
            }
            UseWaitCursor = false;
        }

        private void but_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void load_form()
        {
            PCPUB.m_fill_combo(lst_dyer, "DYER");
            PCPUB.m_fill_combo(lst_transport, "TRANSPORT");
            PCPUB.m_fill_combo(col_name, "YARN_NAME");
            PCPUB.m_fill_combo(col_count, "YARN_COUNT");
            PCPUB.m_fill_combo(col_color, "COLOR");
            PCPUB.m_fill_combo(col_unit, "UNIT_FMTR202");
        }

        private void clear_form()
        {
            dt_tran_date.Value = PCPUB.m_tran_date;
            lst_dyer.SelectedIndex = -1;
            lst_transport.SelectedIndex = -1;
            txt_total_items.Text = String.Empty;
            txt_total_quantity.Text = String.Empty;
            txt_total_kattu.Text = String.Empty;
            txt_total_weight.Text = String.Empty;
            dgv_detail.Rows.Clear();
            txt_tran_id.Text = String.Empty;
            switch (PCPUB.m_entry_form_mode)
            {
                case "ADDNEW":
                    dt_tran_date.Enabled = false;
                    break;
                case "EDIT":
                    dt_tran_date.Enabled = false;
                    fill_form(PCPUB.m_record_date, PCPUB.m_record_id);
                    break;
            }
            lst_dyer.Focus();
        }

        private void fill_form(string p_date, string p_id)
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            OleDbDataReader sqlRead;// = new OleDbDataReader();
            DateTime strReturn = DateTime.Now;
            //           
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            //
            strQuery = String.Empty;
            strQuery += " SELECT CONVERT(DATETIME,[dyeing_iw_date]) dyeing_iw_date,";
            strQuery += " [dyeing_iw_id] dyeing_iw_id,";
            strQuery += " [dyer_id] dyer_id,";
            strQuery += " [transport_id] transport_id";
            strQuery += " FROM [tis_dyeing_iw]";
            strQuery += " WHERE [dyeing_iw_date] = '" +  PCPUB.m_get_sql_date(p_date) + "'";
            strQuery += " AND [dyeing_iw_id] = '" + p_id + "'";
            //
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            if (sqlRead.Read())
            {
                dt_tran_date.Value = (DateTime)sqlRead["dyeing_iw_date"];
                txt_tran_id.Text = sqlRead["dyeing_iw_id"].ToString();
                lst_dyer.SelectedIndex = lst_dyer.FindStringExact(PCPUB.m_get_dyer_name(sqlRead["dyer_id"].ToString()));
                lst_transport.SelectedIndex = lst_transport.FindStringExact(PCPUB.m_get_transport_name(sqlRead["transport_id"].ToString()));
            }
            //
            strQuery = String.Empty;
            strQuery += " SELECT ";
            strQuery += " [yarn_name_id] yarn_name_id,";
            strQuery += " [yarn_count_id] yarn_count_id,";
            strQuery += " [color_id] color_id,";
            strQuery += " [unit_id] unit_id,";
            strQuery += " [quantity] quantity";
            strQuery += " FROM [tis_dyeing_iw_detail]";
            strQuery += " WHERE [dyeing_iw_date] = '" + PCPUB.m_get_sql_date(p_date) + "'";
            strQuery += " AND [dyeing_iw_id] = '" + p_id + "'";
            strQuery += " ORDER BY [dyeing_iw_date],[dyeing_iw_id],[dyeing_iw_detail_id]";
            //
            sqlComm = new OleDbCommand();
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlRead = sqlComm.ExecuteReader();
            //
            dgv_detail.Rows.Clear();
            int r = -1;
            while (sqlRead.Read())
            {
                dgv_detail.Rows.Add();
                r++;
                dgv_detail["col_name",r].Value = sqlRead["yarn_name_id"].ToString();
                dgv_detail["col_count", r].Value = sqlRead["yarn_count_id"].ToString();
                dgv_detail["col_color", r].Value = sqlRead["color_id"].ToString();
                dgv_detail["col_unit", r].Value = sqlRead["unit_id"].ToString();
                dgv_detail["col_quantity", r].Value = sqlRead["quantity"].ToString();
            }
            //
            sqlRead.Close();
            sqlConn.Close();
            sqlRead.Dispose();
            sqlComm.Dispose();
            sqlConn.Dispose();
        }

        private bool validate_form()
        {
            int dgrw = 0;
            int dgrc = 0;
            //String tran_type_id = "FMTR202";
            //String tran_id = String.Empty;
            //String tran_date = String.Empty;
            try
            {
                dgrc = dgv_detail.Rows.Count;
                //if (PCPUB.m_entry_form_mode.Equals("EDIT"))
                //{
                //    tran_id = PCPUB.m_record_id;
                //    tran_date = PCPUB.m_get_sql_date(PCPUB.m_record_date);
                //}
                if (lst_dyer.SelectedIndex == -1)
                {
                    PCMSG.Show("Dyer field should not be empty.");
                    lst_dyer.Focus();
                    return false;
                }
                if (lst_transport.SelectedIndex == -1)
                {
                    PCMSG.Show("Transport field should not be empty.");
                    lst_transport.Focus();
                    return false;
                }
                else if (dgrc <= 1)
                {
                    PCMSG.Show("Table should not be empty.");
                    lst_dyer.Focus();
                    return false;
                }
                else if (dgrc > 1)
                {
                    for (dgrw = 0; dgrw < dgrc - 1; dgrw++)
                    {
                        if (dgv_detail["col_name", dgrw].Value == null)
                        {
                            PCMSG.Show("Yarn Name field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_name", dgrw].Value != null && dgv_detail["col_name", dgrw].Value.ToString().Trim() == String.Empty)
                        {
                            PCMSG.Show("Yarn Name field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_count", dgrw].Value == null)
                        {
                            PCMSG.Show("Yarn Count field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_count", dgrw].Value != null && dgv_detail["col_count", dgrw].Value.ToString().Trim() == String.Empty)
                        {
                            PCMSG.Show("Yarn Count field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_color", dgrw].Value == null)
                        {
                            PCMSG.Show("Color field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_color", dgrw].Value != null && dgv_detail["col_color", dgrw].Value.ToString().Trim() == String.Empty)
                        {
                            PCMSG.Show("Color field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_unit", dgrw].Value == null)
                        {
                            PCMSG.Show("Unit field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_unit", dgrw].Value != null && dgv_detail["col_unit", dgrw].Value.ToString().Trim() == String.Empty)
                        {
                            PCMSG.Show("Unit field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_quantity", dgrw].Value == null)
                        {
                            PCMSG.Show("Quantity field should not be empty.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                        else if (dgv_detail["col_quantity", dgrw].Value != null && (long.Parse(dgv_detail["col_quantity", dgrw].Value.ToString()) <= 0 || dgv_detail["col_quantity", dgrw].Value.ToString().Trim() == String.Empty))
                        {
                            PCMSG.Show("Quantity field should not be empty or zero.");
                            dgv_detail[0, dgrw].Selected = true;
                            return false;
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                PCMSG.ShowError(ex);
                return false;
            }
        }
        
        private bool save_form()
        {
            try
            {
                String strQuery = String.Empty;
                ArrayList alQueries = new ArrayList();
                OleDbConnection sqlConn = new OleDbConnection();
                OleDbCommand sqlComm = new OleDbCommand();
                OleDbTransaction sqlTran;
                //
                String dyeing_iw_id = String.Empty;
                String dyeing_iw_date = String.Empty;
                String user_id = String.Empty;
                String user_datetime = String.Empty;
                String dyer_id = String.Empty;
                String transport_id = String.Empty;
                //
                String dyeing_iw_detail_id = String.Empty;
                String yarn_name_id = String.Empty;
                String yarn_count_id = String.Empty;
                String color_id = String.Empty;
                String unit_id = String.Empty;
                String quantity = String.Empty;
                String kattu = String.Empty;
                String weight = String.Empty;
                //
                int dgrw = 0;
                int dgrc = 0;
                //int dgcm = 0;
                //int dgcc = 0;
                dgrc = dgv_detail.Rows.Count;
                //
                user_id = "'" + PCPUB.m_user_id + "'";
                user_datetime = "'" + PCPUB.m_get_sql_datetime() + "'";
                dyer_id = "'" + ((m_combo_item)lst_dyer.SelectedItem).combo_code.ToString() + "'";
                transport_id = "'" + ((m_combo_item)lst_transport.SelectedItem).combo_code.ToString() + "'";
                //
                switch (PCPUB.m_entry_form_mode)
                {
                    case "ADDNEW":
                        dyeing_iw_id = "'" + PCPUB.m_get_next_sequence("DYEING_IW",dt_tran_date) + "'";
                        dyeing_iw_date = "'" + PCPUB.m_get_sql_date(dt_tran_date) + "'";
                        strQuery = String.Empty;
                        strQuery += "INSERT INTO [tis_dyeing_iw] (";
                        strQuery += "[dyeing_iw_date],";
                        strQuery += "[dyeing_iw_id],";
                        strQuery += "[user_id],";
                        strQuery += "[user_datetime],";
                        strQuery += "[dyer_id],";
                        strQuery += "[transport_id]";
                        strQuery += ") VALUES (";
                        strQuery += dyeing_iw_date + ",";
                        strQuery += dyeing_iw_id + ",";
                        strQuery += user_id + ",";
                        strQuery += user_datetime + ",";
                        strQuery += dyer_id + ",";
                        strQuery += transport_id + ");";
                        alQueries.Add(strQuery);
                        break;
                    case "EDIT":
                        dyeing_iw_id = "'" + PCPUB.m_record_id + "'";
                        dyeing_iw_date = "'" + PCPUB.m_get_sql_date(PCPUB.m_record_date) + "'";
                        strQuery = String.Empty;
                        strQuery += " UPDATE [tis_dyeing_iw] SET";
                        strQuery += " [user_id] = " + user_id + ",";
                        strQuery += " [user_datetime] = " + user_datetime + ",";
                        strQuery += " [dyer_id] = " + dyer_id + ",";
                        strQuery += " [transport_id] = " + transport_id;
                        strQuery += " WHERE [dyeing_iw_date] = " + dyeing_iw_date;
                        strQuery += " AND [dyeing_iw_id] = " + dyeing_iw_id + ";";
                        alQueries.Add(strQuery);
                        break;
                }
                //
                strQuery = String.Empty;
                strQuery += "DELETE FROM [tis_dyeing_iw_detail] ";
                strQuery += " WHERE [dyeing_iw_date] = " + dyeing_iw_date;
                strQuery += " AND [dyeing_iw_id] = " + dyeing_iw_id + ";";
                alQueries.Add(strQuery);
                //
                if (dgrc > 1)
                {
                    for (dgrw = 0; dgrw < dgrc - 1; dgrw++)
                    {
                        dyeing_iw_detail_id = "'" + PCPUB.m_get_next_sequence("DYEING_IW_DETAIL") + "'";
                        yarn_name_id = "'" + dgv_detail[0, dgrw].Value.ToString() + "'";
                        yarn_count_id = "'" + dgv_detail[1, dgrw].Value.ToString() + "'";
                        color_id = "'" + dgv_detail[2, dgrw].Value.ToString() + "'";
                        unit_id = "'" + dgv_detail[3, dgrw].Value.ToString() + "'";
                        quantity =  dgv_detail[4, dgrw].Value.ToString();
                        kattu = dgv_detail["col_kattu", dgrw].Value.ToString();
                        weight = dgv_detail["col_weight", dgrw].Value.ToString();
                        //
                        strQuery = String.Empty;
                        strQuery += "INSERT INTO [tis_dyeing_iw_detail] (";
                        strQuery += "[dyeing_iw_date],";
                        strQuery += "[dyeing_iw_id],";
                        strQuery += "[dyeing_iw_detail_id],";
                        strQuery += "[yarn_name_id],";
                        strQuery += "[yarn_count_id],";
                        strQuery += "[color_id],";
                        strQuery += "[unit_id],";
                        strQuery += "[quantity],";
                        strQuery += "[kattu],";
                        strQuery += "[weight]";
                        strQuery += ") VALUES (";
                        strQuery += dyeing_iw_date + ",";
                        strQuery += dyeing_iw_id + ",";
                        strQuery += dyeing_iw_detail_id + ",";
                        strQuery += yarn_name_id + ",";
                        strQuery += yarn_count_id + ",";
                        strQuery += color_id + ",";
                        strQuery += unit_id + ",";
                        strQuery += quantity + ",";
                        strQuery += kattu + ",";
                        strQuery += weight + ");";
                        alQueries.Add(strQuery);
                    }
                }
                //
                sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
                sqlConn.Open();
                sqlTran = sqlConn.BeginTransaction(); ;
                sqlComm.Connection = sqlConn;
                sqlComm.Transaction = sqlTran;
                //
                try
                {
                    for (int i = 0; i < alQueries.Count; i++)
                    {
                        sqlComm.CommandText = alQueries[i].ToString();
                        sqlComm.ExecuteNonQuery();
                    }
                    sqlTran.Commit();
                }
                catch (Exception ex)
                {
                    sqlTran.Rollback();
                    PCMSG.ShowError(ex);
                }
                //
                PCPUB.m_dyeing_iw_id = dyeing_iw_id;
                sqlConn.Close();
                sqlComm.Dispose();
                sqlConn.Dispose();
                return true;
            }
            catch (Exception e)
            {
                PCMSG.Show(e.Message);
                return false;
            }
        }

        private void update_total_values()
        {
            double item_weight = 0;
            double item_kattu = 0;
            int total_items = 0;
            int total_quantity = 0;
            double total_kattu = 0;
            double total_weight = 0;
            //
            int dgrw = 0;
            int dgrc = 0;
            dgrc = dgv_detail.Rows.Count;
            //
            total_items = dgrc - 1;
            for (dgrw = 0; dgrw < dgrc - 1; dgrw++)
            {
                item_weight = 0;
                item_kattu = 0;
                if (dgv_detail["col_quantity", dgrw].Value != null)
                    total_quantity += int.Parse(dgv_detail[4, dgrw].Value.ToString());
                if (dgv_detail["col_quantity", dgrw].Value != null)
                {
                    item_kattu = double.Parse(dgv_detail["col_quantity", dgrw].Value.ToString()) * PCDB.m_get_unit_conversion(dgv_detail["col_unit", dgrw].Value.ToString(), "KATTU");
                    item_weight = double.Parse(dgv_detail["col_quantity", dgrw].Value.ToString()) * PCDB.m_get_unit_conversion(dgv_detail["col_unit", dgrw].Value.ToString(), "KILOGRAM");
                    dgv_detail["col_weight", dgrw].Value = item_weight;
                    dgv_detail["col_kattu", dgrw].Value = item_kattu;
                    total_weight += item_weight;
                    total_kattu += item_kattu;
                }
            }
            txt_total_items.Text = total_items.ToString();
            txt_total_quantity.Text = total_quantity.ToString();
            txt_total_kattu.Text = total_kattu.ToString();
            txt_total_weight.Text = total_weight.ToString("######0.000");
        }

        private void dgv_detail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3 || e.ColumnIndex == 4)
            {
                update_total_values();
            }
        }

    }

}